import { _decorator, Button, Component, director, instantiate, Label, Node, Prefab, Animation, Vec3, UITransform, macro } from 'cc';
import { AudioMgr } from './AudioMgr';
import { CELL_WIDTH, CELL_WIDTH_MARGIN } from './ConstValue';
const { ccclass, property } = _decorator;

@ccclass('EffecLayer')
export class EffecLayer extends Component {

    @property(Prefab)
    private bombWhite: Prefab
    @property(Prefab)
    private crushEffect: Prefab


    protected onLoad(): void {
        // test
        // this.schedule(() => {
        //     console.debug("play crush")
        //     this.playEffects([
        //         {
        //             action: "crush",
        //             playTime: 0.3,
        //             pos: {
        //                 x: 3,
        //                 y: 3
        //             }
        //         }
        //     ])
        // }, 1, macro.REPEAT_FOREVER)
    }

    start() {

    }

    public playEffects(effectQueue) {
        console.debug(effectQueue)
        if (effectQueue == null && effectQueue.length <= 0) {
            return;
        }
        const soundMap = {}; // 用于标记某一时刻某一种声音是否播放过
        console.debug("playEffects")
        effectQueue.forEach((cmd) => {
            // 使用 setTimeout 来实现延迟
            setTimeout(() => {
                let animation: Animation | null = null;
                let instantEffect: Node = null;
                if (cmd.action == "rowBomb" || cmd.action == "colBomb") { // 行消除
                    let row = cmd.action === "rowBomb"

                    instantEffect = instantiate(this.bombWhite);
                    animation = instantEffect.getComponent(Animation);
                    animation.on(Animation.EventType.FINISHED, () => {
                        console.debug("colplayEffects destroy anim")
                        instantEffect.destroy();
                    }, this);

                    console.debug("colplayEffects play anim")

                    if (instantEffect) {
                        let x = 0;
                        let y = 0;
                        if (row) {
                            x = this.node.getComponent(UITransform).width / 2
                            y = CELL_WIDTH * (cmd.pos.y - 0.5)
                        } else {
                            x = (CELL_WIDTH * (cmd.pos.x - 0.5)) + CELL_WIDTH_MARGIN
                            y = this.node.getComponent(UITransform).height / 2
                        }
                        instantEffect.setWorldPosition(new Vec3(x, y, 0));
                        this.node.addChild(instantEffect);
                    }
                    if (row) {
                        animation?.play('effect_line');
                    } else {
                        animation.play('effect_col');
                    }
                } else if (cmd.action == "crush") { // 普通消除
                    instantEffect = instantiate(this.crushEffect);
                    animation = instantEffect.getComponent(Animation);
                    let x = (CELL_WIDTH * (cmd.pos.x - 0.5)) + CELL_WIDTH_MARGIN;
                    let y = CELL_WIDTH * (cmd.pos.y - 0.5);
                    instantEffect.setPosition(x, y)
                    instantEffect.parent = this.node;
                    animation.on(Animation.EventType.FINISHED, () => {
                        instantEffect.destroy();
                    }, this);
                    animation.play("effect");
                    !soundMap["crush" + cmd.playTime] && AudioMgr.inst.playEliminate(cmd.step);
                    soundMap["crush" + cmd.playTime] = true;
                }
            }, cmd.playTime * 1000); // cmd.playTime 应该以秒为单位，setTimeout 以毫秒为单位
        });
    }
}